package com.hqyj.springboot.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.hqyj.springboot.util.RedisUtil;
import com.xpand.starter.canal.annotation.*;
import javax.annotation.Resource;

@CanalEventListener
public class CanalDataEventListener {

    @Resource
    private RedisUtil redisUtil;

    /**
     * @Description: 增加数据监听
     * @Author: yangjj_tc
     * @Date: 2022/11/11 15:16
     */
    @InsertListenPoint
    public void onEventInsert(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        rowData.getAfterColumnsList()
                .forEach((c) -> System.out.println("By--Annotation: " + c.getName() + " ::   " + c.getValue()));
    }

    /**
     * @Description: 修改数据监听
     * @Author: yangjj_tc
     * @Date: 2022/11/11 15:17
     */
    @UpdateListenPoint
    public void onEventUpdate(CanalEntry.RowData rowData) {
        System.out.println("UpdateListenPoint");
        rowData.getAfterColumnsList()
                .forEach((c) -> System.out.println("By--Annotation: " + c.getName() + " ::   " + c.getValue()));
    }

    /**
     * @Description: 删除数据监听
     * @Author: yangjj_tc
     * @Date: 2022/11/11 15:17
     */
    @DeleteListenPoint
    public void onEventDelete(CanalEntry.EventType eventType) {
        System.out.println("DeleteListenPoint");
    }

    /**
     * @Description: 自定义数据监听
     * @Author: yangjj_tc
     * @Date: 2022/11/11 15:18
     */
    @ListenPoint(destination = "example", schema = "springboot", table = {"users"},
            eventType = CanalEntry.EventType.UPDATE)
    public void onEventCustomUpdate(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        System.err.println("DeleteListenPoint");
        rowData.getAfterColumnsList()
                .forEach((c) -> System.out.println("By--Annotation: " + c.getName() + " ::   " + c.getValue()));
    }

    @ListenPoint(destination = "example", schema = "springboot", // 所要监听的数据库名
            table = {"users"}, // 所要监听的数据库表名
            eventType = {CanalEntry.EventType.UPDATE, CanalEntry.EventType.INSERT, CanalEntry.EventType.DELETE})
    public void onEventCustomUpdateForTbUser(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        //getChangeValue(eventType, rowData);
        System.out.println("检测到users表触发了插入，更新或者删除操作.....");
        rowData.getAfterColumnsList()
                .forEach((c) -> System.out.println("By--Annotation: " + c.getName() + " ::   " + c.getValue()));
        System.out.println("-----------执行删除redis缓存--------");
        redisUtil.removeByPrex("usersCacheList");
    }

    public static void getChangeValue(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        if (eventType == CanalEntry.EventType.DELETE) {
            rowData.getBeforeColumnsList().forEach(column -> {
                // 获取删除前的数据
                System.out.println(column.getName() + " == " + column.getValue());
            });
        } else {
            rowData.getBeforeColumnsList().forEach(column -> {
                // 打印改变前的字段名和值
                System.out.println(column.getName() + " == " + column.getValue());
            });
            rowData.getAfterColumnsList().forEach(column -> {
                // 打印改变后的字段名和值
                System.out.println(column.getName() + " == " + column.getValue());
            });
        }
    }
}

